Skip to content

Fork/Join framework

It's a new framework for async multithread task operation introduced by Java 1.7 using the processor cores to split the operations and run it in a very fast mode. It is basically a different implementation of ExecutorService interface.

Main concepts

  • The operation is divided into two phases:
  • fork phase where the problem is divided in more simply tasks until the task is quite simple to be executed by an async operation
  • join phase where all the results executed by the async tasks are retrieved, all merged into the final result
  • Fork/Join framework use a pool ForkJoinPool that manage thread defined as ForkJoinWorkerThread objects.

ForkJoin pool

It's a pool that manage WorkerThread instances. The pool doesn't create a new thread for each worker, but each thread have a queue of tasks that are executed by the worker from the tail of the queue. The interesting thing is that if a worker have completed its tasks, it can execute tasks defined on bottom of other busy worker's queue. In this way the probability that two threads will be in concurrency for a task is reduced. This process is called Work Stealing Algorithm.